package concurrency;

/**
 * 编码的变体(二)
 *
 * 2018年 09月 24日 星期一
 * @author fireway
 */
public class SelfManaged implements Runnable {
    private int mCountDown = 5;

    private Thread mThread = new Thread(this);

    public SelfManaged() {
        mThread.start();
    }

    @Override
    public String toString() {
        String s = Thread.currentThread().getName() + "(" + mCountDown + "), ";
        return s;
    }

    @Override
    public void run() {
        while(true) {
            System.out.print(this);
            if (0 == --mCountDown) {
                return;
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new SelfManaged();
        }
    }
}
